/**
 * Copyright &copy; 2015-2020 <a href="http://www.easyhis.com/">easyhis</a> All rights reserved.
 */
package com.easy.modules.iim.service.impl;

import com.easy.common.utils.StringUtils;
import com.easy.core.persistence.Page;
import com.easy.core.service.impl.BaseServiceImpl;
import com.easy.modules.iim.entity.LayGroup;
import com.easy.modules.iim.entity.LayGroupUser;
import com.easy.modules.iim.mapper.LayGroupMapper;
import com.easy.modules.iim.mapper.LayGroupUserMapper;
import com.easy.modules.iim.service.LayGroupService;
import com.easy.modules.sys.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 群组Service
 * @author lgf
 * @version 2016-08-07
 */
@Service
@Transactional(readOnly = true)
public class LayGroupServiceImpl  extends BaseServiceImpl implements LayGroupService {
    /**
     * 持久层对象
     */
    @Autowired
    protected LayGroupMapper mapper;

    @Autowired
	private LayGroupUserMapper layGroupUserMapper;
	
	public LayGroup get(String id) {
		LayGroup layGroup = mapper.get(id);
		return layGroup;
	}

	public List<LayGroupUser> getUsersByGroup (LayGroup layGroup){
		return  layGroupUserMapper.findList(new LayGroupUser(layGroup));
	}
	
	public List<LayGroup> findList(LayGroup layGroup) {
		List<LayGroup> layGroupList= new ArrayList<LayGroup>();
		List<LayGroup> list = mapper.findList(layGroup);
		for(LayGroup u:list){
			layGroupList.add(this.get(u.getId()));
		}
		return layGroupList;
	}
	
	public List<LayGroup> findGroupList(User user) {
		List<LayGroup> layGroupList= new ArrayList<LayGroup>();
		LayGroupUser layGroupUser = new LayGroupUser();
		layGroupUser.setUser(user);
		List<LayGroupUser> list = layGroupUserMapper.findList(layGroupUser);
		for(LayGroupUser u:list){
			layGroupList.add(this.get(u.getGroup().getId()));
		}
		return layGroupList;
	}
	

	@Transactional(readOnly = false)
	public void save(LayGroup layGroup) {
		if (layGroup.getIsNewRecord()){
			layGroup.preInsert();
			mapper.insert(layGroup);
		}else{
			layGroup.preUpdate();
			mapper.update(layGroup);
		}

		for (LayGroupUser layGroupUser : layGroup.getLayGroupUserList()){
			if (layGroupUser.getId() == null){
				continue;
			}
			if (LayGroupUser.DEL_FLAG_NORMAL.equals(layGroupUser.getDelFlag())){
				if (StringUtils.isBlank(layGroupUser.getId())){
					layGroupUser.setGroup(layGroup);
					layGroupUser.preInsert();
					layGroupUserMapper.insert(layGroupUser);
				}else{
					layGroupUser.preUpdate();
					layGroupUserMapper.update(layGroupUser);
				}
			}else{
				layGroupUserMapper.delete(layGroupUser);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(LayGroup layGroup) {
		mapper.delete(layGroup);
		layGroupUserMapper.delete(new LayGroupUser(layGroup));
	}


	/**
	 * 获取单条数据
	 * @param entity
	 * @return
	 */
	public LayGroup get(LayGroup entity) {
		return mapper.get(entity);
	}



	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param entity
	 * @return
	 */
	public Page<LayGroup> findPage(Page<LayGroup> page, LayGroup entity) {
		dataRuleFilter(entity);
		entity.setPage(page);
		page.setList(mapper.findList(entity));
		return page;
	}




	/**
	 * 删除全部数据
	 * @param entitys
	 */
	@Transactional(readOnly = false)
	public void deleteAll(Collection<LayGroup> entitys) {
		for (LayGroup entity : entitys) {
			mapper.delete(entity);
		}
	}

	/**
	 * 删除全部数据
	 * @param entitys
	 */
	@Transactional(readOnly = false)
	public void deleteAllByLogic(Collection<LayGroup> entitys) {
		for (LayGroup entity : entitys) {
			mapper.deleteLogic(entity);
		}
	}


	/**
	 * 获取单条数据
	 * @param propertyName, value
	 * @return
	 */
	public LayGroup findUniqueByProperty(String propertyName, Object value){
		return mapper.findUniqueByProperty(propertyName, value);
	}

	/**
	 * 动态sql
	 * @param sql
	 */

	public List<Object> executeSelectSql(String sql){
		return mapper.execSelectSql(sql);
	}

	@Transactional(readOnly = false)
	public void executeInsertSql(String sql){
		mapper.execInsertSql(sql);
	}

	@Transactional(readOnly = false)
	public void executeUpdateSql(String sql){
		mapper.execUpdateSql(sql);
	}

	@Transactional(readOnly = false)
	public void executeDeleteSql(String sql){
		mapper.execDeleteSql(sql);
	}

}